package com.ttt.gdfood.mapper;

import com.ttt.gdfood.entity.Note;
import org.apache.ibatis.annotations.*;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Mapper
public interface NoteMapper {
    // @Select("select n.content,n.thumbs_up,n.comment,n.collection,n.update_time,u.username,u.avatar,i.src,n.id from note n LEFT JOIN user u ON n.uid=u.id LEFT JOIN images i ON n.id=i.note_id")
    @Select("SELECT id,note_title,content,thumbs_up,comment,collection,region,create_time,update_time,uid FROM note ORDER BY id DESC")
    @Results({
            @Result(property = "user", column = "uid", one = @One(select = "com.ttt.gdfood.mapper.UserMapper.findById")),
            @Result(property = "images", column = "id", many = @Many(select = "com.ttt.gdfood.mapper.ImageMapper.findById")),
            @Result(property = "contacts", column = "uid", many = @Many(select = "com.ttt.gdfood.mapper.ContactMapper.getNoteContact")),
            @Result(property = "collections", column = "id", many = @Many(select = "com.ttt.gdfood.mapper.CollectionMapper.getUid")),
            @Result(property = "thumbsUps", column = "id", many = @Many(select = "com.ttt.gdfood.mapper.ThumbsUpMapper.getUid")),
            @Result(property = "id",column = "id"),
            @Result(property = "uid",column = "uid")
    })
    @Transactional
    List<Note> noteAll();

    @Select("SELECT note_title,content,thumbs_up,comment,collection,id,region,update_time,uid FROM note WHERE region=#{region} ORDER BY id DESC")
    @Results({
            @Result(property = "user", column = "uid", one = @One(select = "com.ttt.gdfood.mapper.UserMapper.findById")),
            @Result(property = "images", column = "id", many = @Many(select = "com.ttt.gdfood.mapper.ImageMapper.findById")),
            @Result(property = "contacts", column = "uid", many = @Many(select = "com.ttt.gdfood.mapper.ContactMapper.getNoteContact")),
            @Result(property = "collections", column = "id", many = @Many(select = "com.ttt.gdfood.mapper.CollectionMapper.getUid")),
            @Result(property = "thumbsUps", column = "id", many = @Many(select = "com.ttt.gdfood.mapper.ThumbsUpMapper.getUid")),
            @Result(property = "id",column = "id"),
            @Result(property = "uid",column = "uid")
    })
    @Transactional
    List<Note> noteByRegion(String region);

    @Select("SELECT note_title,content,thumbs_up,comment,collection,id,region,update_time,uid FROM note WHERE uid=#{id}")
    @Results({
            @Result(property = "user", column = "uid", one = @One(select = "com.ttt.gdfood.mapper.UserMapper.findById")),
            @Result(property = "images", column = "id", many = @Many(select = "com.ttt.gdfood.mapper.ImageMapper.findById")),
            @Result(property = "contacts", column = "uid", many = @Many(select = "com.ttt.gdfood.mapper.ContactMapper.getNoteContact")),
            @Result(property = "collections", column = "id", many = @Many(select = "com.ttt.gdfood.mapper.CollectionMapper.getUid")),
            @Result(property = "thumbsUps", column = "id", many = @Many(select = "com.ttt.gdfood.mapper.ThumbsUpMapper.getUid")),
            @Result(property = "id",column = "id"),
            @Result(property = "uid",column = "uid")
    })
    @Transactional
    List<Note> noteById(int id);

    @Select("SELECT note_title,content,thumbs_up,comment,collection,id,region,update_time,uid FROM note WHERE id=#{id}")
    @Results({
            @Result(property = "user", column = "uid", one = @One(select = "com.ttt.gdfood.mapper.UserMapper.findById")),
            @Result(property = "images", column = "id", many = @Many(select = "com.ttt.gdfood.mapper.ImageMapper.findById")),
            @Result(property = "contacts", column = "uid", many = @Many(select = "com.ttt.gdfood.mapper.ContactMapper.getNoteContact")),
            @Result(property = "collections", column = "id", many = @Many(select = "com.ttt.gdfood.mapper.CollectionMapper.getUid")),
            @Result(property = "thumbsUps", column = "id", many = @Many(select = "com.ttt.gdfood.mapper.ThumbsUpMapper.getUid")),
            @Result(property = "id",column = "id"),
            @Result(property = "uid",column = "uid")
    })
    @Transactional
    Note noteByNoteId(int id);

    @Select("SELECT note_title,content,thumbs_up,comment,collection,id,region,update_time,uid FROM note WHERE note_title like CONCAT('%',#{keyword},'%') ORDER BY id DESC")
    @Results({
            @Result(property = "user", column = "uid", one = @One(select = "com.ttt.gdfood.mapper.UserMapper.findById")),
            @Result(property = "images", column = "id", many = @Many(select = "com.ttt.gdfood.mapper.ImageMapper.findById")),
            @Result(property = "contacts", column = "uid", many = @Many(select = "com.ttt.gdfood.mapper.ContactMapper.getNoteContact")),
            @Result(property = "collections", column = "id", many = @Many(select = "com.ttt.gdfood.mapper.CollectionMapper.getUid")),
            @Result(property = "thumbsUps", column = "id", many = @Many(select = "com.ttt.gdfood.mapper.ThumbsUpMapper.getUid")),
            @Result(property = "id",column = "id"),
            @Result(property = "uid",column = "uid")
    })
    @Transactional
    List<Note> noteByKeyWord(String keyWord);

    @Delete("delete from note where id=#{id}")
    @Transactional
    void deleteById(int id);

    @Insert("INSERT INTO note(note_title,content,thumbs_up,`comment`,collection,region,create_time,update_time,uid) VALUES(#{note_title},#{content},#{thumbs_up},#{comment},#{collection},#{region},#{create_time},#{update_time},#{uid})")
    @Options(useGeneratedKeys = true,keyProperty = "id",keyColumn = "id")
    @Transactional
    void addNote(Note note);
}
